package com.loong.system.controller.backend;

import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.global.dto.AdminDTO;
import com.loong.system.common.backend.param.SystemLetterPageQueryParam;
import com.loong.system.common.backend.param.SystemLetterParam;
import com.loong.system.handler.SystemLetterHandler;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 处理后台站内信相关请求的controller
 */
@RestController
@RequestMapping("manage/system/letter")
public class SystemLetterController extends BaseBackendController {
    @Resource
    private SystemLetterHandler letterHandler;

    /**
     * 回复反馈时的新增站内信
     * @param letterParam 站内信参数
     * @return 响应的结果
     */
    @PostMapping("save/repliedLetter")
    public Result saveRepliedLetter(@Validated({CommonInsert.class})SystemLetterParam letterParam){
        letterParam.setId(null);
        AdminDTO adminDTO = super.getAdminDTO();
        letterParam.setSender(adminDTO.getUsername());
        letterParam.setSenderId(adminDTO.getId());
        return handlerExecutor.execute(() -> letterHandler.saveRepliedLetter(letterParam));
    }

    /**
     * 更新站内信
     * @param letterParam 站内信参数
     * @return 响应的结果
     */
    @PostMapping("update")
    public Result updateLetter(@Validated({CommonUpdate.class})SystemLetterParam letterParam){
        return handlerExecutor.execute(() -> letterHandler.updateLetter(letterParam));
    }

    /**
     * 批量删除站内信
     * @param idList 站内信id集合
     * @return 响应的结果
     */
    @PostMapping("batchDelete")
    public Result batchDeleteLetter(@RequestParam("idList") List<Long> idList){
        if (CollectionUtils.isEmpty(idList)) return null;
        return handlerExecutor.execute(() -> letterHandler.batchDeleteLetter(idList));
    }

    /**
     * 分页查询站内信
     * @param queryParam 查询参数
     * @return 响应的结果
     */
    @PostMapping("pageQuery/LetterList")
    public Result pageQueryLetter(SystemLetterPageQueryParam queryParam){
        return handlerExecutor.execute(() -> letterHandler.pageQueryLetter(queryParam));
    }



}
